package com.dragon.modules.yuegou.service.impl.merchant;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.common.exception.DragonException;
import com.dragon.common.web.PageRequest;
import com.dragon.modules.yuegou.entity.busd.BtypeEntity;
import com.dragon.modules.yuegou.entity.merchant.MerchantLevelRuleEntity;
import com.dragon.modules.yuegou.entity.merchant.ThemeEntity;
import com.dragon.modules.yuegou.enums.code.MerchantCode;
import com.dragon.modules.yuegou.enums.merchant.ThemeIsDefault;
import com.dragon.modules.yuegou.enums.merchant.ThemeStatus;
import com.dragon.modules.yuegou.enums.merchant.ThemeType;
import com.dragon.modules.yuegou.mapper.merchant.ThemeMapper;
import com.dragon.modules.yuegou.model.busd.Btype;
import com.dragon.modules.yuegou.model.merchant.Theme;
import com.dragon.modules.yuegou.query.busd.BtypeQuery;
import com.dragon.modules.yuegou.query.merchant.MerchantLevelRuleQuery;
import com.dragon.modules.yuegou.query.merchant.ThemeQuery;
import com.dragon.modules.yuegou.service.busd.BtypeService;
import com.dragon.modules.yuegou.service.merchant.MerchantLevelRuleService;
import com.dragon.modules.yuegou.service.merchant.ThemeService;
import com.dragon.modules.yuegou.utils.PageToResultUtils;
import com.dragon.modules.yuegou.web.Check;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 模板管理 服务实现
 *
 * @author pbuilder generated
 * @date 2019-09-23 15:31:49
 * @version 2.0
 */
@Slf4j
@Service("merThemeService")
public class ThemeServiceImpl
		extends ServiceImpl<ThemeMapper, ThemeEntity>
		implements ThemeService {


	@Autowired
	private BtypeService btypeService;

	@Autowired
	private MerchantLevelRuleService merchantLevelRuleService;
	/**
	 * 分页查询所有主题及模糊查询所有的模板名称相符的
	 * @param page
	 * @return
	 */
	@Override
	public IPage<Theme> searchList(PageRequest page, String name) {
		ThemeQuery themeQuery = new ThemeQuery();
		themeQuery.name().like(name);
		QueryWrapper<ThemeEntity> query = themeQuery.toQueryWrapper();
		query.orderByDesc(ThemeEntity.COL_CREATED_TIME);
		return page(PageToResultUtils.resultToPage(page), query);
	}

	/**
	 * 根据主题id查询具体的参数
	 * @param id
	 * @return
	 */
	@Override
	public Theme searchOne(String id) {
		Check.notNull(id,"id不能为空！！");
		ThemeQuery themeQuery = new ThemeQuery();
		themeQuery.setId(id);
		return new Theme(getOne(themeQuery.toQueryWrapper()));
	}
	/**
	 * 根据id修改模板的上下架状态
	 * @param id
	 */
	@Override
	public void shelveAndDown(String id) {
		Theme theme = searchOne(id);
		if (ThemeStatus.DOWN.equals(theme.getStatus())) {
			theme.setStatus(ThemeStatus.PUTAWAY);
		} else {
			theme.setStatus(ThemeStatus.DOWN);
		}
		updateById(theme);
	}

	/**
	 * 保存主题模板新建或修改
	 * @param query
	 */
	@Override
	public void saveTheme(Theme query) {
		Check.notNull(query,"对象为空请重新填写");
		Check.notBlank(query.getName(),"模板名称不能为空");
		Check.notNull(query.getType(),"模板类型不能为空");
		Check.notNull(query.getLevelId(),"可使用等级不能为空");
		Check.notNull(query.getStatus(),"状态不能为空");
		Check.notNull(query.getStoreDemoImg(),"悦主页效果图不能为空");
		Check.notNull(query.getStoreStyleImgMain(),"悦主页背景样式不能为空");
		Check.notNull(query.getStoreStyleImgRepeat(),"悦主页背景样式平铺图不能为空");
		Check.notNull(query.getBtnRightColor(),"右边按钮色值不能为空");
		Check.notNull(query.getDetailDemoImg(),"详情页效果图不能为空");
		Check.notNull(query.getDetailStyleImgMain(),"详情页背景样式主图不能为空");
		Check.notNull(query.getDetailStyleImgRepeat(),"详情页背景样式平铺图不能为空");

        if (query.getType().getCode() == 0 && Objects.isNull(query.getFormId())) {
			log.debug("模板类型为常用模板必须填写业态");
			throw new DragonException(MerchantCode.FORM_IS_NO_EMPTY);
        }
		MerchantLevelRuleQuery merchantLevelRuleQuery = new MerchantLevelRuleQuery();
		merchantLevelRuleQuery.setId(query.getLevelId());
		MerchantLevelRuleEntity one = merchantLevelRuleService.getOne(merchantLevelRuleQuery.toQueryWrapper());
		query.setLevelName(one.getLevelName());
		if (!Objects.equals(query.getType(), ThemeType.ACTIVETEMPLATE)) {
			BtypeQuery btypeQuery = new BtypeQuery();
			btypeQuery.setId(query.getFormId());
			BtypeEntity one1 = btypeService.getOne(btypeQuery.toQueryWrapper());
			query.setFormName(one1.getName());
		}
		if (Objects.isNull(query.getIsDefault())) {
			query.setIsDefault(ThemeIsDefault.NO);
		}

		if ( StringUtils.isEmpty(query.getId())) {
			query.setId(IdWorker.getIdStr());
			save(query);
			return;
		}
		updateById(query);

	}

	/**
	 * 业态信息模糊搜索 主题模板列表
	 * @param page
	 * @param formName
	 * @return
	 */
	@Override
	public IPage<Theme> templateList(PageRequest page, String formName) {
		ThemeQuery themeQuery = new ThemeQuery();
		themeQuery.setFormName(formName);
		themeQuery.setStatus(ThemeStatus.PUTAWAY);
		return baseMapper.templateList(PageToResultUtils.resultToPage(page), themeQuery);
	}

	@Override
	public List<Btype> searchFormName() {
		BtypeQuery btypeQuery = new BtypeQuery();
		List<BtypeEntity> list = btypeService.list(btypeQuery.toQueryWrapper());
		return list.stream().map(Btype::new).collect(Collectors.toList());
	}

}